#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <stdlib.h>

#include "resample.h"

int resample_16k_to_8k(int16_t *ibuf, int16_t *obuf, uint32_t ilen, uint32_t *polen)
{
    int i = 0;

    if(NULL == ibuf || NULL == obuf || 0 == ilen || NULL == polen)
    {
        return -1;
    }

    for(i = 0; i < ilen / 2; i++)
    {
        obuf[i] = ibuf[2 * i];
    }

    *polen = ilen / 2;
    return 0;
}

int resample_8k_to_16k(int16_t *ibuf, int16_t *obuf, uint32_t ilen, uint32_t *polen)
{
    int i = 0;

    if(NULL == ibuf || NULL == obuf || 0 == ilen || NULL == polen)
    {
        return -1;
    }

    for(i = 0; i < ilen - 1; i++)
    {
        obuf[2 * i] = ibuf[i];
        obuf[2 * i + 1] = (ibuf[i] + ibuf[i + 1]) / 2;
    }
    obuf[ilen * 2 - 2] = ibuf[ilen - 1];
    obuf[ilen * 2 - 1] = ibuf[ilen - 1];

    *polen = ilen * 2;
    return 0;
}

#ifdef __cplusplus
}
#endif

